package com.affectu.task.action;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.component.html.HtmlPanel;

import com.affectu.dao.DAOBase;
import com.affectu.task.Bid;
import com.affectu.task.InnerTask;
import com.affectu.task.OuterTask;
import com.affectu.task.Task;

/*
 * 2008-8-24 
 * @author daniel nathan
 */
@Name("taskViewAction")
@Scope(ScopeType.EVENT)
public class TaskViewAction extends DAOBase {

	// private String id;
	private Task task;
	private HtmlPanel htmlPanelBid;

	public String viewAction(Task task) {
		try {
			this.task = task;
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "/task/view.xhtml";
	}

	public Task getTask() {
		try {
			Object taskType = this.getRequestParam().get("taskType");
			Object taskId = this.getRequestParam().get("taskId");
			if (taskType == null && taskId == null) {
				taskType = this.getSessionScope().get("taskType");
				taskId = this.getSessionScope().get("taskId");
			} else {
				this.getSessionScope().put("taskType", taskType);
				this.getSessionScope().put("taskId", taskId);
			}

			if (taskType != null && taskId != null) {
				if (Task.Type.INNER.toString().endsWith(taskType.toString())) {
					task = (InnerTask) em.get(InnerTask.class, Long
							.valueOf(taskId.toString()));
				} else if (Task.Type.OUTER.toString().endsWith(
						taskType.toString())) {
					task = (OuterTask) em.get(OuterTask.class, Long
							.valueOf(taskId.toString()));
				} else {// pre-leave

				}
			}
		} catch (Exception ex) {
			this.logException(ex);
		}
		return task;
	}

	public int getBetweenDays() {// 跨年需修正
		int days = 0;
		try {
			Calendar pubDate = new GregorianCalendar();
			if (this.getTask() != null
					&& this.getTask().getPublishDate() != null) {
				pubDate.setTime(this.getTask().getPublishDate());
				Calendar today = Calendar.getInstance();
				days = today.get(Calendar.DAY_OF_YEAR)
						- pubDate.get(Calendar.DAY_OF_YEAR);
			}
		} catch (Exception ex) {
			this.logException(ex);
		}
		return days;
	}

	public String bidShow() {
		this.getHtmlPanelBid().setRendered(true);
		return "";
	}

	@SuppressWarnings("unchecked")
	public String bidSubmit(Bid bid) {
		try {
			List<Bid> bs = em
					.createSQLQuery(
							"select b.* from Bid b where b.userId=:userId and b.taskId=:taskId")
					.addEntity("b", Bid.class).setParameter("userId",
							this.getCurrentUser().getId()).setParameter(
							"taskId", this.getTask().getId()).list();
			if (bs.size() > 0) {
				bs.get(0).setDescription(bid.getDescription());// for update
				this.getTask().getAllBid().add(bs.get(0));
			} else {
				bid.setOwner(this.getCurrentUser());
				bid.setBelongToTask((InnerTask) this.getTask());
				this.getTask().getAllBid().add(bid);// for add
			}
			em.merge(this.getTask());

			FacesMessages.instance().add("投标成功!");
			this.getHtmlPanelBid().setRendered(false);
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "";// "/task/index.xhtml";
	}

	public HtmlPanel getHtmlPanelBid() {
		return htmlPanelBid;
	}

	public void setHtmlPanelBid(HtmlPanel htmlPanelBid) {
		this.htmlPanelBid = htmlPanelBid;
	}

	public void destroy() {
		// this.getSessionScope().remove("taskId");
	}
}
